package com.zcs.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.zcs.order.service.DouyinService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * @author zhangchunsheng
 * @version 1.0.0
 * @className DouyinServiceImpl
 * @date 2024/11/10   22:51
 * @description TODO
 */
@Service
public class DouyinServiceImpl implements DouyinService {


    @Override
    public String getCode() {
        try {
            // 创建URL对象
            String getParam = "client_key=" + "awqm7adiq6vo8l5y"
                    + "&" + "response_type=" + "code"
                    + "&" + "scope=" + "user_info,friend_relation,trial.whitelist,video.list,renew_refresh_token"
                    + "&" + "redirect_uri=" + "https://192.168.2.68/callback"
//                    + "&" + "response_type=" + "code"
//                    + "&" + "response_type=" + "code"
//                    + "&" + "response_type=" + "code"
//                    + "&" + "response_type=" + "code"
                    ;//拼接参数
            URL obj = new URL("https://open.douyin.com/platform/oauth/connect");
            // 打开连接
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            // 设置请求方法为GET
            con.setRequestMethod("GET");
            // 获取响应代码
            int responseCode = con.getResponseCode();
//                    System.out.println("响应代码：" + responseCode);

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            System.out.println("response: " + response);
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String getToken(String code) {
        String args1 = "client_key=" + "awqm7adiq6vo8l5y"
                + "&" + "client_secret=" + "b17f3d9baf88a3f796fc94f9a615a366"
                + "&" + "code=" + "9d28d3da7033d14csmoH473hDvtldoBxSx4M_hl"
                + "&" + "grant_type=" + "authorization_code";
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String access_token = "";
        String open_id = "";
        try {
            String url = "https://open.douyin.com/oauth/access_token/?"+args1;

            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
            // 解析微信服务器的响应
            String responseBody = response.getBody();
            Map m = JSON.parseObject(responseBody, Map.class);
                Map dataMap = JSON.parseObject(m.get("data")+"", Map.class);
                System.out.println(dataMap);
                access_token  = dataMap.get("access_token") + "";
                open_id  = dataMap.get("open_id") + "";
                System.out.println(access_token);

            getVidoList(access_token,open_id);
            return access_token;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return access_token;

    }

    @Override
    public String getVidoList(String token,String open_id) {
        try {
            // 创建URL对象
            String getParam =
                    "open_id="+open_id
                    +"&"+"cursor=0&count=10"
                    ;//拼接参数
            URL obj = new URL("https://open.douyin.com/api/douyin/v1/video/video_list/?"+getParam);
            // 打开连接
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            // 设置请求方法为GET
            con.setRequestMethod("GET");
            con.setRequestProperty("access-token",token);
            con.setRequestProperty("content-type","application/json");
            // 获取响应代码
            int responseCode = con.getResponseCode();
//                    System.out.println("响应代码：" + responseCode);

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            System.out.println("response: " + response);
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}